package aufgabe2;

import java.security.Principal;
import java.security.acl.*;
import java.util.HashMap;

import sun.security.acl.PrincipalImpl;

public class Acl extends sun.security.acl.AclImpl {

    /** the groups that are available in this application **/
    private HashMap<String, Group> groups;

    /**
     * sets up the ACL with groups, permissions, and ACL-entries
     */
    public Acl() {
        super(new PrincipalImpl("root"), "UniManager Access Control List");

        // defining groups
        Group users = new sun.security.acl.GroupImpl("users");
        Group students = new sun.security.acl.GroupImpl("students");
        Group teachers = new sun.security.acl.GroupImpl("teachers");
        Group tutors = new sun.security.acl.GroupImpl("tutors");
        Group admins = new sun.security.acl.GroupImpl("admins");

        groups = new HashMap<String, Group>();
        groups.put(users.toString(), users);
        groups.put(students.toString(), students);
        groups.put(teachers.toString(), teachers);
        groups.put(tutors.toString(), tutors);
        groups.put(admins.toString(), admins);

        users.addMember(students);
        users.addMember(teachers);
        users.addMember(tutors);
        teachers.addMember(admins);

        // defining aclEntries
        AclEntry userPositives = new sun.security.acl.AclEntryImpl(users);
        AclEntry teacherPositives = new sun.security.acl.AclEntryImpl(teachers);
        AclEntry adminPositives = new sun.security.acl.AclEntryImpl(admins);

        // defining permissions
        Permission coursesList = new sun.security.acl.PermissionImpl(
                "coursesList");
        Permission courseSubscribeStudent = new sun.security.acl.PermissionImpl(
                "courseSubscribeStudent");
        Permission courseListStudents = new sun.security.acl.PermissionImpl(
                "courseListStudents");
        Permission courseEditSettings = new sun.security.acl.PermissionImpl(
                "courseEditSettings");
        Permission courseUnsubscribeStudent = new sun.security.acl.PermissionImpl(
                "courseUnsubscribeStudent");
        Permission courseAddTeacher = new sun.security.acl.PermissionImpl(
                "courseAddTeacher");

        // add permissions to entries
        userPositives.addPermission(coursesList);
        userPositives.addPermission(courseSubscribeStudent);
        userPositives.addPermission(courseUnsubscribeStudent);

        teacherPositives.addPermission(courseListStudents);
        teacherPositives.addPermission(courseEditSettings);

        adminPositives.addPermission(courseAddTeacher);

        try {
            this.addEntry(new PrincipalImpl("root"), userPositives);
            this.addEntry(new PrincipalImpl("root"), teacherPositives);
            this.addEntry(new PrincipalImpl("root"), adminPositives);

        } catch (NotOwnerException e) {
            e.printStackTrace();
            System.out.println("i didnt set correctly");
        }
    }

    /**
     * adds a user (principal) to a certain group
     * 
     * @param user
     *            the principal to add
     * @param groupName
     *            the group to which the principal is added
     */
    public void addUserToGroup(Principal user, String groupName) {
        groups.get(groupName).addMember(user);
    }

}
